package org.qf.web;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.qf.entity.SmbmsUser;
import org.qf.service.SmbmsUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
public class SmbmsUserController {

    //调用业务层
    @Resource
    private SmbmsUserService smbmsUserService;

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){

        //作用域：page request(一次会话，只要项目不停上，这个项目的每个页面可以共享session中的数据)applicatio)

        SmbmsUser smbmsUser = smbmsUserService.login(username,password);
log.info("{}",smbmsUser);

    System.out.println(smbmsUser);
        if(smbmsUser != null){
            //将对象存储到session中
            session.setAttribute("user",smbmsUser);
            //代表业务层
            return "welcome";
        }else {
            //代表业务层
            return "login";
        }
    }



    @RequestMapping("/showList")
    public String showList(@RequestParam(defaultValue = "1") int pageNum, String name, Model model){
        SmbmsUser smbmsUser = new SmbmsUser();
        smbmsUser.setUserName(name);
        PageInfo<SmbmsUser> pageInfo = smbmsUserService.showList(pageNum,smbmsUser);
        if(pageInfo.getList().size()>0){
            //有数据跳转前端
            //将数据存储在model中，那么跳转的页面就可以使用数据
            model.addAttribute("pageInfo",pageInfo);//将数据存储在model中
            model.addAttribute("name",name);
            return "userList";
        }else {
            return "error";
        }
    }



    @RequestMapping("/addUser")
    public String addUser(){
        return "userAdd";
    }


    @RequestMapping("/add")
    public String add(SmbmsUser smbmsUser){log.info("smbmsUser{}",smbmsUser);
        int i = smbmsUserService.addSmbmsUser(smbmsUser);

        if(i>0){
            return "redirect:/showList";
        }else {
            return "redirect:/addUser";
        }
    }



    @RequestMapping("/viewUser")
    public String viewUser(int id,Model model){
        SmbmsUser smbmsUser = smbmsUserService.showUserById(id);
        if(smbmsUser != null){
            model.addAttribute("s",smbmsUser);
            return  "userView";
        }else {
            return "error";
        }
    }


    //跳转到修改页面
    @RequestMapping("/updateView")
    public String updateView(int id,Model model){
        SmbmsUser smbmsUser = smbmsUserService.showUserById(id);
        if(smbmsUser != null){
            model.addAttribute("ss",smbmsUser);
            return  "userUpdate";
        }else {
            return "error";
        }
    }


    //修改
    @RequestMapping("/updateUser")
    public String updateUsr(SmbmsUser smbmsUser){
        int i = smbmsUserService.updateUser(smbmsUser);
        if(i > 0){
            return "redirect:/showList";
        }else{
            return "error";
        }
    }

    //删除
    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
        int i = smbmsUserService.deleteUser(id);
        if(i > 0){
            return "redirect:/showList";
        }else{
            return "error";
        }
    }

    @RequestMapping("viewPwd")
    public String viewPwd(int id, Model model){
        log.info("{}",id);
        SmbmsUser smbmsUser = smbmsUserService.showUserById(id);
        if(smbmsUser != null){
            model.addAttribute("p",smbmsUser);
            return "password";
        }else {
            return "error";
        }
    }



    @RequestMapping("updatePwd")
    public String updatePwd(SmbmsUser smbmsUser){
        int i = smbmsUserService.updateUser(smbmsUser);
        if(i > 0){
            return "redirect:/showList";
        }else {
            return "error";
        }
    }



}
